package com.cssy.xxkj.basic.web;


import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.cssy.base.web.BaseController;
import com.cssy.base.web.BodyMsg;
import com.cssy.xxkj.basic.entity.BasicMaterialsDO;
import com.cssy.xxkj.basic.entity.BasicMaterialsTypeDO;
import com.cssy.xxkj.basic.service.IBasicMaterialsService;
import com.cssy.xxkj.basic.service.IBasicMaterialsTypeService;
import com.cssy.xxkj.basic.service.IBasicUtilService;
import com.plat.exception.SyBaseException;
import com.plat.helper.JqGridHandler;
import com.plat.helper.SearchCondition;
import com.plat.helper.SearchConditions;
import com.plat.helper.SearchResult;
import com.plat.util.GlobalResLoader;
import com.plat.util.OperationFlag;
import com.plat.util.SessionUtil;
import com.plat.util.json.OutJsonGrid;
import com.yuendong.sy.po.code.SyCode;
import com.yuendong.sy.service.code.impl.SyCodeService;
@Controller
@RequestMapping("/basic/materials")
public class MaterialsController extends BaseController<BasicMaterialsDO>{
	private static final String EIDT = "/xxkj/basic/materials/MaterialsOperation";//添加、修改、显示页面
	private static final String MANAGER = "/xxkj/basic/materials/MaterialsManager";
	
	@Resource(name="basicMaterialsTypeService")
	private IBasicMaterialsTypeService basicMaterialsTypeService;
	
	@Resource(name="basicMaterialsService")
	private IBasicMaterialsService basicMaterialsService;
	
	//主数据公用集合Service
	@Resource(name="basicUtilService")
	private IBasicUtilService basicUtilService;
	
	@Resource(name="syCodeService")
	private SyCodeService syCodeService;
	
	public String manager(HttpServletRequest request, Model model)
			throws Exception {
		return MANAGER;
	}
	
	
	@Override
	public void list(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String ID = request.getParameter("ID");
		JqGridHandler hander=new JqGridHandler(request);
		SearchConditions scs = new SearchConditions();
		hander.getWheres(scs);
		SearchResult result = new SearchResult();
		if(StringUtils.isNotBlank(ID)){
			scs.addCondition("zd029MaterTypeId", ID, SearchCondition.OP_EQUAL);
		}
		result = basicMaterialsService.findListByExample(scs);
		OutJsonGrid out=new OutJsonGrid(result);
		out.printJSon(response);
		
	}
	
	@Override
	public String operationForm(HttpServletRequest request,@PathVariable String id,
			@PathVariable String flag, Model model) throws Exception {
		if(OperationFlag.ADD.equals(flag)){//我新增加时操作
			BasicMaterialsTypeDO parent =basicMaterialsTypeService.findListById(id);
			model.addAttribute("parent", parent);
			model.addAttribute("flag", "add");
		}else{
			BasicMaterialsDO bd =basicMaterialsService.findListById(id);
			BasicMaterialsTypeDO parent =basicMaterialsTypeService.findListById(bd.getZd029MaterTypeId());
			model.addAttribute("parent", parent);
			model.addAttribute("materials", bd);
			model.addAttribute("flag", "edit");
		}
		SearchConditions scs = new SearchConditions();
		scs.setRemovedProperty(null);
		//默认仓库
		model.addAttribute("wh", basicUtilService.resultStorage(scs));
		//单位
		model.addAttribute("unit", basicUtilService.resultStapleUnit(scs));
		//材质
		model.addAttribute("quality", basicUtilService.resultMaterialQuality(scs));
		return EIDT;
	}
	
	
	@Override
	public Map<String, Object> save(HttpServletRequest request,
			BasicMaterialsDO curAP, BindingResult result, Model model)
	{
		
		BodyMsg bodyMap=BodyMsg.getInstance();
		try {
				if(curAP.getId()!=null && !"".equals(curAP.getId())){//修改
					basicMaterialsService.update(curAP, SessionUtil.getUser(request));
					basicMaterialsService.saveAttach(curAP, request);
					bodyMap.isSuccess();
					bodyMap.configWebInfor(GlobalResLoader.getOpsuccess());
					bodyMap.configWebId(curAP.getId());
				}else{//新增
				
					BasicMaterialsDO cdo=null;
					try {
						cdo=basicMaterialsService.save(curAP, SessionUtil.getUser(request));
						basicMaterialsService.saveAttach(cdo, request);
					} catch (Exception e) {
						e.printStackTrace();
						bodyMap.isFail();
						bodyMap.configWebInfor("数据出现重复条目，请检查后重新输入");
						return bodyMap.modelMap;
					}
					bodyMap.isSuccess();
					bodyMap.configWebInfor(GlobalResLoader.getOpsuccess());
					if(cdo!=null){
						bodyMap.configWebId(cdo.getId());
					}
				}
		} catch (Exception e) {			
			e.printStackTrace();
			bodyMap.isFail();
			bodyMap.configWebInfor(GlobalResLoader.getOpfailed()+e.getMessage());
		}
		return bodyMap.modelMap;
		
	}
		

	@Override
	public Map<String, Object> delete(HttpServletRequest request,
			HttpServletResponse response, Model model) throws Exception {
		BodyMsg bodyMap = BodyMsg.getInstance();
		try {
			String isDelete = request.getParameter("isDelete");//标识是否是假删除
			String ids = request.getParameter("ids");
			String[] fields=ids.split(",");
			for(String field : fields){
				if(isDelete==null||"0".equals(isDelete)){
					basicMaterialsService.delete(field, SessionUtil.getUser(request));
				}else{
					basicMaterialsService.deleteBizRemoved(field);
				}
			}
			bodyMap.isSuccess(GlobalResLoader.getOpsuccess());
		} catch (Exception e) {
			e.printStackTrace();
			bodyMap.isFail(GlobalResLoader.getOpfailed() + e.getMessage());
		}
		return bodyMap.modelMap;
	}
	
	public List<BasicMaterialsDO> getParetName(String parentId,List<BasicMaterialsDO> basicMaterialsDOs) throws SyBaseException {
		if(parentId != null &&  parentId.length() > 0){
			SearchConditions scs = new SearchConditions();
			scs.addCondition("id", parentId,SearchCondition.OP_EQUAL);
			List<BasicMaterialsDO> list =basicMaterialsService.findListByExample(scs).getResults();
			basicMaterialsDOs.add(list.get(0));
		}
		return basicMaterialsDOs;
	}
	
//	@RequestMapping("/exportExcel")
//	public void exportAll(HttpServletRequest request,
//			HttpServletResponse response) throws Exception {
//		List<BasicMaterialsDO> list = null;
//		String idlist = null;
//		JqGridHandler hander = new JqGridHandler(request);
//		SearchConditions scs = new SearchConditions();
//		scs.clearCondition();
//		hander.setFilters(new String(request.getParameter("filters").getBytes(
//				"ISO-8859-1"), "UTF-8")); // 转码
//		hander.getWheres(scs);
//		scs.setLimitClause(null);
//		scs.setRemovedProperty("removed");//判断是否有删除标识
//		String ID =request.getParameter("ID");
//	//	hander.getWheres(scs);	
//		if(ID!=null&&!"".equals(ID)){
//			List<BasicMaterialstypeDO> list1 = basicmatertypeService.findByParentId(ID);
//			if(list1.size()>0){
//				idlist = idList(list1,ID);
//				scs.addCondition("itmetypeid", "\'"+idlist+"\'", SearchCondition.OP_IN);
//			}else{
//				scs.addCondition("itmetypeid", ID, SearchCondition.OP_EQUAL);
//			}
//		}
//		if(scs.getOrderBy()==null||"".equals(scs.getOrderBy()))
//			 scs.setOrderBy("code desc");
//		scs.addCondition("typeName", "原材料物料",SearchCondition.OP_EQUAL);
//		SearchResult result=	basicMaterialsService.findListByExample(scs);
//		list = result.getResults();
//		ExcelUtil.toExcel(list, "物料料号:code┋物料名称:name┋物料规格:spec┋单位:unit┋描述:description┋财务分类:remark1┋备注:remark","原材料物料列表导出",response);
//	}
	
	
}
